layui.define(['myCommon'], function (exports) {
    "use strict";

    let $ = layui.jquery
        , form = layui.form
        , myCommon = layui.myCommon;

    window.popupWindow = function (name, url, callback, width, height) {
        if (typeof callback != 'function') {
            callback = function () {
            }
        }
        if (width == null) {
            width = 1200;
        }
        if (height == null) {
            height = 650;
        }
        myCommon.showWindow({
            title: name,
            url: myCommon.url(url),
            dialogWidth: width,
            dialogHeight: height,
            fnCallback: function (ret) {
                callback(ret);
            }
        });
        return false;
    };

    let myApp = {
        getCityListByProvinceId: function (provinceId, fn) {
            let $cityId = $('#cityId')
                , cityId = $cityId.val()
                , html = ['<option value="">选择城市</option>'];
            if (provinceId == '') {
                $cityId.find('option').remove();
                $('#townId').find('option').remove();
                form.render('select');
                return;
            }
            myCommon.request.get({
                url: 'base.area/getCityListByProvinceId'
                , data: {
                    provinceId: provinceId
                }
                , prefix: true
            }, function (res) {
                if (fn != null) {
                    return fn(res);
                }
                $cityId.find('option').remove();
                $('#townId').find('option').remove();
                $.each(res.data, function (i, n) {
                    if (n.id == cityId) {
                        html.push('<option value="' + n.id + '" selected>' + n.name + '</option>');
                    } else {
                        html.push('<option value="' + n.id + '">' + n.name + '</option>');
                    }
                });
                $cityId.html(html.join(''));
                form.render('select');
            })
        }
    };

    exports('myApp', myApp);
});